package hard.demo01;


/**
 * Created with IntelliJ IDEA.
 *
 * @Author: wfmiss
 * @Date: 2023/12/12/14:56
 * @Description:
 * 题目来源：力扣 https://leetcode.cn/problems/add-two-numbers/
 * 2. 两数相加
 * 给你两个 非空 的链表，表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的，并且每个节点只能存储 一位 数字。
 * 请你将两个数相加，并以相同形式返回一个表示和的链表。
 * 你可以假设除了数字 0 之外，这两个数都不会以 0 开头。
 */
public class Solution {

    public static void main(String[] args) {
        /**
         * 测试用例：
         * 示例 1：
         * 2 ——> 4 ——> 3
         * 5 ——> 6 ——> 4
         * 7 ——> 0 ——> 8
         * 输入：l1 = [2,4,3], l2 = [5,6,4]
         * 输出：[7,0,8]
         * 解释：342 + 465 = 807.
         *
         * 示例 2：
         * 输入：l1 = [0], l2 = [0]
         * 输出：[0]
         *
         * 示例 3：
         * 输入：l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
         * 输出：[8,9,9,9,0,0,0,1]
         */
        ListNode listNode1 = new ListNode(1);
        // ListNode listNode2 = new ListNode(8);
        // ListNode listNode3 = new ListNode(3);
        // ListNode listNode4 = new ListNode(9);
        // ListNode listNode5 = new ListNode(9);
        // ListNode listNode6 = new ListNode(9);
        // ListNode listNode7 = new ListNode(9);
        // listNode1.next = listNode2;
        // listNode2.next = listNode3;
        // listNode3.next = listNode4;
        // listNode4.next = listNode5;
        // listNode5.next = listNode6;
        // listNode6.next = listNode7;
        ListNode p = listNode1;
        while (p.next != null) {
            System.out.print(p.val);
            p = p.next;
        }
        System.out.println(p.val);

        ListNode listNode12 = new ListNode(9);
        ListNode listNode22 = new ListNode(9);
        // ListNode listNode32 = new ListNode(9);
        // ListNode listNode42 = new ListNode(9);
        listNode12.next = listNode22;
        // listNode22.next = listNode32;
        // listNode32.next = listNode42;
        ListNode p2 = listNode12;
        while (p2.next != null) {
            System.out.print(p2.val);
            p2 = p2.next;
        }
        System.out.println(p2.val);
        ListNode x = addTwoNumbers(listNode1, listNode12);
        while (x.next != null) {
            System.out.print(x.val);
            x = x.next;
        }
        System.out.println(x.val);
    }


    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode p1 = l1;
        ListNode p2 = l2;
        ListNode sum = new ListNode(0);
        ListNode p3 = sum;
        int ad = 0;
        int x=0;
        while (p1.next != null || p2.next != null) {
            if (p1.next != null && p2.next != null) {
                if ((p1.val + p2.val + ad) < 10) {
                    p3.val = p1.val + p2.val + ad;
                    ad = 0;
                }
                if ((p1.val + p2.val + ad) >= 10) {
                    p3.val = (p1.val + p2.val+ ad) % 10 ;
                    ad = 1;
                }
                p1 = p1.next;
                p2 = p2.next;
                ListNode s = new ListNode(0);
                p3.next = s;
                p3 = s;
            }
            if (p1.next != null && p2.next == null&&x==0) {
                if ((p1.val + p2.val + ad) < 10) {
                    p3.val = p1.val + p2.val + ad;
                    ad = 0;
                }
                if ((p1.val + p2.val + ad) >= 10) {
                    p3.val = (p1.val + p2.val+ ad) % 10 ;
                    ad = 1;
                }
                p1 = p1.next;
                ListNode s = new ListNode(0);
                p3.next = s;
                p3 = s;
                x=1;
            }
            if (p1.next != null && p2.next == null&& x==1) {
                if((p1.val+ad)>=10){
                    p3.val=(p1.val+ad)%10;
                    ad=1;
                }else {
                    p3.val=p1.val+ad;
                    ad=0;
                }
                p1 = p1.next;
                ListNode s = new ListNode(0);
                p3.next = s;
                p3 = s;
            }
            if (p1.next == null && p2.next != null&&x==0) {
                if ((p1.val + p2.val + ad) < 10) {
                    p3.val = p1.val + p2.val + ad;
                    ad = 0;
                }
                if ((p1.val + p2.val + ad) >= 10) {
                    p3.val = (p1.val + p2.val+ ad) % 10 ;
                    ad = 1;
                }
                p2 = p2.next;
                ListNode s = new ListNode(0);
                p3.next = s;
                p3 = s;
                x=2;
            }
            if (p1.next == null && p2.next != null&& x==2) {
                if((p2.val+ad)>=10){
                    p3.val=(p2.val+ad)%10;
                    ad=1;
                }else {
                    p3.val=p2.val+ad;
                    ad=0;
                }
                p2 = p2.next;
                ListNode s = new ListNode(0);
                p3.next = s;
                p3 = s;
            }
        }

        if(x==1&&ad==1) {
            if((p1.val + ad)>=10){
                p3.val = (p1.val + ad)%10;
                ad=1;
            }
            else {
                p3.val = p1.val + ad;
                ad=0;
            }
        } else if (x==1&&ad==0) {
            p3.val = p1.val + ad;
        } else if (x==2&&ad==1){
            if((p2.val + ad)>=10){
                p3.val = (p2.val + ad)%10;
                ad=1;
            }
            else {
                p3.val = p2.val + ad;
                ad=0;
            }
        }else if (x==2&&ad==0) {
            p3.val = p2.val + ad;
        }else {
            if ((p1.val + p2.val + ad) < 10) {
                p3.val = p1.val + p2.val + ad;
                ad = 0;
            }
            if ((p1.val + p2.val + ad) >= 10) {
                p3.val = (p1.val + p2.val+ ad) % 10 ;
                ad = 1;
            }
        }
        if(ad==1){
            ListNode s = new ListNode(ad);
            p3.next = s;
        }
        return sum;
    }
}
